package com.cy.demo1.controller;

import com.cy.demo1.entity.User;
import com.cy.demo1.service.IUserService;
import com.cy.demo1.service.ex.InsertException;
import com.cy.demo1.service.ex.UsernameDuplicatedException;
import com.cy.demo1.util.JsonResult;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController //@Controller+@ResponseBody
@RequestMapping("users")
public class UserController extends BaseController{

    @Resource
    private IUserService userService;

    @RequestMapping("reg")
    public JsonResult<Void> reg(String username,String password) {
        User user = new User();
        user.setUserName(username);
        user.setPassword(password);
        userService.reg(user);

        return new JsonResult<>(OK);
    }

    @RequestMapping("login")
    @ResponseBody
    public JsonResult<User> login(String username, String password){
        User data = userService.login(username, password);


        return new JsonResult<User>(OK, data);
    }

    @RequestMapping("get_by_uid")
    public JsonResult<User> get_by_uid(Integer userId){
        User data = userService.getByUid(userId);

        return new JsonResult<User>(OK, data);
    }

    @RequestMapping("change_info")
    public JsonResult<User> change_info(Integer userId, String userName, String password){
        userService.changeInfo(userId,userName,password);

        return new JsonResult<User>(OK);
    }
}
